﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Application
open System.Security;

// =======================================================
// Create, update, delete views
// =======================================================

let list = Client.Context.Web.Lists.GetByTitle("Documents")

Client.Load list
Client.Load list.Views

// Retrieve view fields
let nameless = list.Views.GetByTitle("All Documents")
Client.Load nameless
Client.Load nameless.ViewFields
nameless.ListViewXml
for fld in nameless.ViewFields do
    let msg = fld
    printfn "%s\n" msg

// Set as default
nameless.DefaultView <- false
nameless.Update()
Client.ExecuteQuery()

// Delete view
nameless.DeleteObject()
Client.ExecuteQuery()

// Create a new view
let query = CamlQuery.CreateAllItemsQuery()
let newViewInfo = new ViewCreationInformation()
newViewInfo.Paged <- true
newViewInfo.PersonalView <- false
newViewInfo.Query <- query.ViewXml
newViewInfo.RowLimit <- uint32 25
newViewInfo.SetAsDefaultView <- false
newViewInfo.Title <- "Testview"
let flds =     
    [|
    "Edit"
    "Title"
    "LinkTitleNoMenu"
    "LinkTitle"
    "LinkTitle2"
    "SelectTitle"
    "FileRef"
    "FileLeafRef"
    "LinkFilenameNoMenu"
    "LinkFilename"
    "LinkFilename2"
    "PermissionDisp"
    |]
newViewInfo.ViewFields <- flds
let newView = list.Views.Add(newViewInfo)
list.Update()
Client.ExecuteQuery()

list.DefaultView.ViewFields.RemoveAll()
list.DefaultView.Update()
Client.ExecuteQuery()

list.DefaultView.ViewFields.Add("ID")
list.DefaultView.ViewFields.Add("Status")
list.DefaultView.Update()
Client.ExecuteQuery()
